from django.shortcuts import render
from django.http import HttpResponse

from App.models import Person
from random import randrange


def add_persons(request):
    for i in range(15):
        person = Person()
        person.name = f'Tom{randrange(999)}'
        person.age = randrange(999)
        person.sex = randrange(999) % 2
        person.save()
    return HttpResponse('添加人员成功')


def get_persons(request):
    persons1 = Person.objects.filter(age__gt=400).filter(age__lt=800)
    persons2 = Person.objects.exclude(age__gt=400).exclude(age__lt=800)
    persons3 = Person.objects.all().order_by('age')
    persons4 = persons3.values()
    content = {
        'person1': persons1,
        'person2': persons2,
        'person3': persons3,
        'person4': persons4,
    }

    return render(request, 'person_list.html', context=content)


def addperson(request):
    person = Person('Jack')
    person.save()
    return HttpResponse('ok')


def getperson(request):
    person = Person.objects.get()

    return HttpResponse('ok')

def get_user(request):

    return HttpResponse('ok')